package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.IPartinActiveAdapter;
import com.wbao.dianniu.data.ActivityListData;
import com.wbao.dianniu.listener.IActivityListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityListManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 我参与的活动
 * 
 * @author yejianjun
 * 
 */
public class IPartInActivity extends BaseActivity implements
		IActivityListListener {
	private ActivityListManager manager;
	private PullToRefreshListView pullListView;
	private ListView listView;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	private List<ActivityListData> mList = new ArrayList<ActivityListData>();
	private IPartinActiveAdapter adapter;
	private TextView noDataTV;
	private static final int TYPE = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_i_partin,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.i_partin));
		initView();
		initData();
	}

	private void initView() {
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		pullListView = (PullToRefreshListView) findViewById(R.id.i_partIn_listview);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		listView = pullListView.getRefreshableView();
		registerForContextMenu(listView);
	}

	private void initData() {
		manager = new ActivityListManager(this);
		manager.addActivityListener(this);
		adapter = new IPartinActiveAdapter(this);
		adapter.addData(mList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(itemClickListener);
		reqData(0);
	}

	private void reqData(int page) {
		manager.activityReq(GlobalContext.accountId, page * PAGE_COUNTS,
				PAGE_COUNTS, "", TYPE);
	}

	OnRefreshListener onRefreshListener = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};

	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		currentPage++;
		reqData(currentPage);
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			 Intent intent = new Intent();
			 intent.putExtra(GlobalContext.INTENT_ACTIVITYID,mList.get(position-1).getId());
			 intent.setClass(IPartInActivity.this, ActivityDetail.class);
			 startActivity(intent);
		}
	};

	@Override
	public void onActivityFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false, false);
		Notification.toast(this, errorMsg);
	}

	@Override
	public void onActivitySuccess(List<ActivityListData> list) {
		pullListView.onRefreshComplete(false, false);
		if (null != list) {
			adapter.addData(list);
			mList = adapter.getList();
			if (adapter.getCount() == 0) {
				noDataTV.setVisibility(View.VISIBLE);
			} else {
				noDataTV.setVisibility(View.GONE);
			}
			adapter.notifyDataSetChanged();
		}
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
